package com.itcast.controller;

import com.itcast.dao.PublishDao;
import com.itcast.dao.PublishDaoImpl;
import com.itcast.pojo.Publish;
import com.itcast.pojo.User;
import com.itcast.service.PublishService;
import com.itcast.service.PublishServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/InsertPublishServlet")
public class InsertPublishServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
       String txt= request.getParameter("content");
        System.out.println(txt);
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");
        PublishService publishService=new PublishServiceImpl();
        Publish publish=new Publish();
        publish.setUserId(user.getUserId());
        publish.setUserName(user.getUserName());
        publish.setPublishTxt(txt);
        publish.setUserImage(user.getUserImage());
        
        publishService.InsertPublish(publish);
        
        PublishDao publishDao=new PublishDaoImpl();
        List<Publish> publishes= publishDao.findAll();
        request.setAttribute("publishes",publishes);
        request.getRequestDispatcher("/findFriendServlet").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
